package com.mmall.service.impl;

import com.google.common.base.Splitter;
import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.dao.CartMapper;
import com.mmall.pojo.Cart;
import com.mmall.service.ICartService;
import com.mmall.util.BigDecimalUtil;
import com.mmall.vo.CartItemProductVo;
import com.mmall.vo.CartVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by root on 18-4-11.
 */
@Service
public class CartServiceImpl implements ICartService {

    @Autowired
    private CartMapper cartMapper;

    public ServerResponse addProduct(Integer userId, Integer productId, Integer count) {
        return saveCart(userId, productId, count);
    }

    public ServerResponse saveCart(Integer userId, Integer productId, Integer count) {
        if (userId == null) {
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        } else if (productId == null || count == null || count < 1){
            return ServerResponse.createByError(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        Cart cartItem = cartMapper.selectByUserProduct(productId, userId);
        if (cartItem == null) {
            cartItem = new Cart();
            cartItem.setChecked(Const.Cart.CHECKED);
            cartItem.setProductId(productId);
            cartItem.setUserId(userId);
            cartItem.setQuantity(count);
            cartMapper.insert(cartItem);
        } else {
            cartItem.setQuantity(count);
            cartMapper.updateByPrimaryKey(cartItem);
        }
        CartVo cartVo = getCartVo(userId);
        return ServerResponse.createBySuccess(cartVo);
    }




    private CartVo getCartVo(Integer userId) {
        List<CartItemProductVo> cartItemProductVos = cartMapper.selectCartProductVoListByUser(userId);
        CartVo cartVo = new CartVo();
        boolean isAllCheck = true;
        BigDecimal cartTotalPrice = new BigDecimal(0);
        if (cartItemProductVos != null && cartItemProductVos.size() > 0) {
            for(CartItemProductVo cartItemProductVo: cartItemProductVos) {
                if (cartItemProductVo.getProductStock() < cartItemProductVo.getQuantity()) {
                    Cart cart = new Cart();
                    cart.setQuantity(cartItemProductVo.getProductStock());
                    cart.setId(cartItemProductVo.getId());
                    cartMapper.updateByPrimaryKey(cart);
                    cartItemProductVo.setQuantity(cartItemProductVo.getProductStock());
                    cartItemProductVo.setLimitQuantity(Const.Cart.LIMIT_NUM_FAIL);
                }
                if (cartItemProductVo.getProductChecked() == 1) {
                    BigDecimalUtil.add(cartTotalPrice.floatValue(), cartItemProductVo.getProductTotalPrice().floatValue());
                } else if (isAllCheck) {
                    isAllCheck = false;
                }
            }
        }
        cartVo.setAllChecked(isAllCheck);
        cartVo.setCartProductVoList(cartItemProductVos);
        cartVo.setCartTotalPrice(cartTotalPrice);
        return cartVo;
    }

    public ServerResponse getCartList(Integer userId) {
        if (userId == null) {
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        }
        CartVo cartVo = getCartVo(userId);
        return ServerResponse.createBySuccess(cartVo);
    }

    public  ServerResponse updateProduct(Integer userId, Integer productId, Integer count) {
        return saveCart(userId, productId, count);
    }

    public ServerResponse deleteProducts(Integer userId, String productIds) {
        if (userId == null) {
            return ServerResponse.createByError(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
        } else if (productIds == null || StringUtils.isBlank(productIds)) {

        }
        List<String> productIdList = Splitter.on(",").splitToList(productIds);
        int rsCount = cartMapper.deleteByUserIdProductIds(productIdList, userId);
        CartVo cartVo = getCartVo(userId);
        return ServerResponse.createBySuccess(cartVo);
    }


}
